package com.manager.common.tcp.encode.impl;

import com.manager.common.tcp.constant.TcpDataType;
import com.manager.common.tcp.encode.BaseEncode;
import io.netty.buffer.ByteBuf;

public class ReceiptEncode extends BaseEncode {

    /**
     * {
     * “serialNumber”: ‘123456789’, //与下发的流水号一致
     * “sate”: 1 // -1:失败，1：成功
     * “reason”: “” //失败原因
     * }
     * */

    @Override
    public byte[] encode(String data) throws Exception {
        byte[] bytes = data.getBytes("UTF-8");
        ByteBuf buf = this.head(TcpDataType.RECEIPT);

        buf.writeInt(bytes.length);
        buf.writeBytes(bytes);

        byte[] result = new byte[buf.readableBytes()];
        buf.readBytes(result);

        return result;
    }
}
